package com.snowsea.listener;

import com.alibaba.fastjson.JSON;
import com.snowsea.pojo.Order;
import com.snowsea.pojo.RedPacket;
import com.snowsea.service.RedPacketService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @className: RedPacketListener
 * @auther: Jerry
 * @date: 2021/4/19 18:10
 * @desctiption: TODO
 * @version: 1.0
 */
@Component
@RocketMQMessageListener(topic = "orderPaySuccessTopic", consumerGroup = "redPacketGroup")
public class RedPacketListener implements RocketMQListener<String> {

    @Autowired
    private RedPacketService redPacketService;

//    @Override
//    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
//        defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
//    }

    @Override
    public void onMessage(String msg) {
        System.out.println("接收到订单系统发来的消息。。。");
        Order order = JSON.parseObject(msg, Order.class);
        RedPacket redPacket = new RedPacket();
        redPacket.setUserId(order.getUserId());
        redPacket.setOrderId(order.getId());
        redPacket.setTotalFee(order.getTotalFee());
        redPacket.setGmtCreate(new Date());
        redPacket.setGmtModified(new Date());
        redPacketService.sellRedPacket(redPacket);
    }

}